define(function (require, exports, module) {
    var oBody = $('body');

    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) {
            return unescape(arr[2]);
        }
        else{
            return null;
        }

    }

    function  JudgeSource(ios, and, wxmin, wx, alipay, qq, h5) {
        var u = window.navigator.userAgent;
        //var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

        var isWXMini = window.__wxjs_environment == 'miniprogram';

        var ua = u.toLowerCase();
        var isWeiXin = ua.match(/MicroMessenger/i) == 'micromessenger';
        var isAlipay = ua.match(/AlipayClient/i) == 'alipayclient';
        var isQQ = ua.match(/QQ/i) == 'qq';

        // 检测室安卓还是ios
        if(u.search('origin/sfddjapp') > 0) {
            if(isIOS) { if(ios) ios(); }
            else { if(and) and(); }
        }

        // 检测小程序
        else if(isWXMini) { if(wxmin) wxmin(); }

        // 检测ddj wx, alipay, qq
        else {
            if(isWeiXin) { if(wx) wx(); }
            else if(isAlipay) { if(alipay) alipay(); }
            else if(isQQ) { if(qq) qq(); }
            else { if(h5) h5(); }
        }
    }

    // 第三方统计的插件
    var TPLMethods = {
        userInfo: JSON.parse( getCookie( 'ddj-userInfo') ),

        // 百度统计
        baidu: function() {
            /**
             *  百度统计
             */
            var sfddjUrl = '9a6219852c6bc360b517de9714e19810'; //m.sfddj.com
            (function () {
                var hm = document.createElement("script");
                hm.src = "//hm.baidu.com/hm.js?" + sfddjUrl;
                // var s = document.getElementsByTagName("script")[0];
                // s.parentNode.insertBefore(hm, s);
                oBody.append(hm);
            })();
        },

        // gio 统计
        gio: function() {
            var _this = this;
            !function (e, t, n, g, i) {
                e[i] = e[i] || function () {
                    (e[i].q = e[i].q || []).push(arguments)
                },
                    n = t.createElement("script");
                // tag = t.getElementsByTagName("script")[0];
                // n.async = 1;
                n.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + g;
                // tag.parentNode.insertBefore(n, tag)
                oBody.append(n);
            }(window, document, "script", "assets.growingio.com/2.1/gio.js", "gio");

            var _gioSetUserId = '';
            if (_this.userInfo) {
                if( _this.userInfo.memberId ) {
                    _gioSetUserId = _this.userInfo.memberId;
                }
                else if( _this.userInfo.openId ) {
                    _gioSetUserId = _this.userInfo.openId;
                }
                else {
                    _gioSetUserId = '';
                }
            }

            gio('init', 'b665fa1721dac33b', {});
            // console.log(ddjhoc.userAction)
            // console.log(gio)
            // 给gio 传递用户信息
            gio('setUserId', _gioSetUserId);
            gio('send');
        },

        // 黑树科技 统计
        blackTree: function() {
            var _this = this;
            var _elqQ = _elqQ || [];
            _elqQ.push(['elqSetSiteId', '980740743']);
            _elqQ.push(['elqTrackPageView']);

            (function () {
                function async_load() {
                    var s = document.createElement('script');
                    s.type = 'text/javascript';
                    s.async = true;
                    s.src = '//img07.en25.com/i/elqCfg.min.js';
                    // var x = document.getElementsByTagName('script')[0];
                    // x.parentNode.insertBefore(s, x);
                    oBody.append(s);
                }
                async_load();
                // if (window.addEventListener) window.addEventListener('DOMContentLoaded', async_load, false);
                // else if (window.attachEvent) window.attachEvent('onload', async_load);
            })();


            var elqCreateAccount = function(data){
                try {
                    var memberid = data.memberId;
                    var emailAddress = memberid + '@dumy.com';
                    var source = data.source;

                    var xhr = new XMLHttpRequest();
                    xhr.open('post', 'https://s980740743.t.eloqua.com/e/f2' );
                    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    xhr.send('elqFormName=无标题表单-1566901174675&elqSiteID=980740743&memberid1='+memberid+'&source1='+source+'&emailAddress='+emailAddress+'&eloquaSync1=1'+'');
                    xhr.onreadystatechange = function () {
                        // console.log(xhr.responseText);
                    };
                }catch(e){
                    console.error('elqCreateAccount', e)
                }
                return true;
            }


            var _BTUserInfo = {
                memberId: '',
                source: ''
            };
            if ( _this.userInfo ) {
                if( _this.userInfo.memberId ) {
                    _BTUserInfo.memberId = _this.userInfo.memberId;
                }
                // else if( _this.userInfo.openId ) {
                //     _BTUserInfo.memberId = _this.userInfo.openId;
                // }
                else {
                    _BTUserInfo.memberId = '';
                }
                // _BTUserInfo.source = _this.userInfo.loginChannel;
            }

            // JudgeSource(ios, and, wxmin, wx, alipay, qq, h5)
            JudgeSource(function () {
                _BTUserInfo.source = 'IOS_DDJAPP';
            }, function () {
                _BTUserInfo.source = 'Android_DDJAPP';
            }, function () {
                _BTUserInfo.source = 'WXMin';
            }, function () {
                _BTUserInfo.source = 'WeiXinClient';
            }, function () {
                _BTUserInfo.source = 'AlipayClient';
            }, function () {
                _BTUserInfo.source = 'QQClient';
            }, function () {
                _BTUserInfo.source = 'H5_BROWSER';
            });

            if(_BTUserInfo.memberId && _BTUserInfo.source) {
                // console.log('BT--数据齐全', _BTUserInfo)
                elqCreateAccount(_BTUserInfo);
            }

        }
    };


    // ZaneMa Testing
    $.getScript("/static_v4/js/plug/DDJStatistics.js", function(response,status) {
        ddjhoc.init(function () {
            TPLMethods.baidu();
            TPLMethods.gio();
            TPLMethods.blackTree();
        });
    });
});



